package com.g4.shoppingback.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.g4.shoppingback.pojo.DdKindType;
import com.g4.shoppingback.mapper.DdKindTypeMapper;
import com.g4.shoppingback.service.IDdKindTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.g4.shoppingback.common.PageVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 型号列表 服务实现类
 * </p>
 *
 * @author admin
 * @since 2022-02-19
 */
@Service
@Slf4j
@Cacheable(value = "true")
public class DdKindTypeServiceImpl extends ServiceImpl<DdKindTypeMapper, DdKindType> implements IDdKindTypeService {

    @Autowired
    private DdKindTypeMapper ddKindTypeMapper;

//    增加商品
    @Override
    @Transactional
    public void addPd(DdKindType ddKindType) {
        //入库商品表
        ddKindTypeMapper.insert(ddKindType);

    }
    //删除商品详情
    @Override
    @Transactional
    public void deleteById(Integer id) {
        //删除指定商品配置
        ddKindTypeMapper.deleteById(id);
    }

    //修改商品配置信息
    @Override
    public void updateSta(DdKindType ddKindType) {

        ddKindTypeMapper.updateById(ddKindType);

    }

    //通过id更新商品配置状态
    @Override
    @Transactional
    public void updateItem(Integer id , Integer state) {

        ddKindTypeMapper.updateItem(id,state);

    }



    //分页查询商品表配置
    @Override
    public PageVo getKindType(PageVo pageVo) {
        //用条件选择器 设置条件
        QueryWrapper<DdKindType> queryWrapper = new QueryWrapper<>();

        //根据后台人员输入的数据Query中提取的pd_name属性进行模糊查询
        boolean flag = StringUtils.hasLength(pageVo.getQuery());
        queryWrapper.like(flag, "pd_name",pageVo.getQuery());

        //用MP包下的 Page 设置 查询的页数  和   条数  ,可自己设置
        Page<DdKindType> page = new Page<>(pageVo.getPageNum(), 5);

        //执行条件构造器中的条件, page自动获取结果
        page=ddKindTypeMapper.selectPage(page, queryWrapper);

        //获取记录数
        long total = page.getTotal();

        List<DdKindType> rows = page.getRecords();

        //返回用户查询的数据的 总数 和  结果
        return pageVo.setTotal(total).setRows(rows);
    }


    @Override
    public DdKindType selectById(Integer id) {

        DdKindType ddKindType = ddKindTypeMapper.selectById(id);

        return ddKindType;
    }
}
